package com.springboot.frame.excel.handler;

import org.apache.poi.ss.usermodel.Cell;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author madman
 */
public class CustomEnumCellHandler extends AbstractBaseCellHandler<Enum> {

  /**
   * 自定义枚举处理器实例
   */
  static final CustomEnumCellHandler INSTANCE = new CustomEnumCellHandler();

  /**
   * 获取枚举值的方法
   */
  private static Method method;

  static {
    try {
      method = EnumValueProvider.class.getDeclaredMethod("getValue");
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
  }

  /**
   * Cell设值
   *
   * @param cell Cell对象
   * @param data 数据
   */
  @Override
  public void doWrite(Cell cell, Enum data) {
    if (data != null) {
      try {
        cell.setCellValue((String) method.invoke(data));
      } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Cell读值
   *
   * @param cell Cell对象
   * @return Cell对象上的值
   * @throws UnsupportedOperationException 不支持操作异常
   */
  @Override
  public Enum read(Cell cell, Object object) {
    throw new UnsupportedOperationException();
  }

}
